﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aandelendelen.Db;
using Aandelendelen.Obs;
using Aandelendelen.Mail;

namespace Aandelendelen.Admin
{
	public partial class AdminAdMaintenance : AdminBaseForm
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			string strId = Request.QueryString["id"];

			if (strId != null)
			{
				int id = Convert.ToInt32(strId);

				string del = Request.QueryString["del"];
				if (del == "1")
					this.DeleteCampaign(id);
				else
					this.ActivateCampaign(id);
			}

			this.PopulateCampaigns();
		}

		private void DeleteCampaign(int id)
		{
			Query query = new Query("sp_deletead", "id", id, System.Data.OleDb.OleDbType.Integer);

			query.Execute();
		}

		private void ActivateCampaign(int id)
		{
			Query query = new Query("u_activatead", "id", id, System.Data.OleDb.OleDbType.Integer);

			int rows = (int)query.Execute();

			if (rows == 1)
			{
				Query getUserQuery = new Query("s_userfromad", "id", id, System.Data.OleDb.OleDbType.Integer);

				ResultSet resultSet = (ResultSet)getUserQuery.Execute();

				if (resultSet.Count != 1)
					return;

				AdvertiserInfo userInfo = new AdvertiserInfo(resultSet);

				MailArguments mailArgs = MailArguments.MailArgs;
				mailArgs.Clear();

				mailArgs["{aanhef}"] = userInfo.MailAanhef;
				mailArgs["{id}"] = id.ToString();

				EMail email = MailCache.GetMail("adactivated");
				email.Recipients = userInfo.Email;

				email.QueueSingle();
			}
		}

		private void PopulateCampaigns()
		{
			Query query = new Query("s_allads");

			ResultSet resultSet = (ResultSet)query.Execute();

			for (int i = 0; i < resultSet.Count; i++)
			{
				this.PopulateCampaign(resultSet, i);
			}
		}

		private void PopulateCampaign(ResultSet resultSet, int row)
		{
			this.CreateBannerRow(resultSet, row);

			this.CreateDetailRow(resultSet, row);
		}

		private void CreateBannerRow(ResultSet resultSet, int row)
		{
			string banner = (string)resultSet[row, "bannerurl"];
			bool adsense = (banner == "adsense");
			string targetUrl = (string)resultSet[row, "targeturl"];
			string toolTip = (string)resultSet[row, "tooltip"];

			TableRow tableRow = new TableRow();

			TableCell bannerCell = new TableCell();
			bannerCell.CssClass = "myadbanner";
			bannerCell.ColumnSpan = 4;
			if (adsense == false)
			{
				HyperLink link = new HyperLink();
				link.ImageUrl = banner;
				link.Text = toolTip;
				link.NavigateUrl = targetUrl;
				link.Target = "_blank";

				bannerCell.Controls.Add(link);
			}
			else
				bannerCell.Text = "adsense";

			tableRow.Cells.Add(bannerCell);

			TableCell stateCell = new TableCell();
			stateCell.ColumnSpan = 6;
			bool paidFor = (bool)resultSet[row, "paidfor"];
			DateTime dt = (DateTime)resultSet[row, "enddate"];
			bool active = (paidFor == true && dt.Year == 1980);
			Label lbl = new Label();
			lbl.Text = (active ? "Actief " : "Niet actief ");
			stateCell.Controls.Add(lbl);
			stateCell.CssClass = (active ? "myadstateactive" : "myadstateinactive");

			if (paidFor == false)
			{
				HyperLink activateLink = new HyperLink();
				activateLink.Text = "Activeren";
				activateLink.CssClass = "common";
				activateLink.NavigateUrl = HttpContext.Current.Request.Url.AbsolutePath + "?id=" + resultSet[row, "id"].ToString(); ;
				stateCell.Controls.Add(activateLink);
			}

			if (!active)
			{
				HyperLink deleteLink = new HyperLink();
				deleteLink.Text = "Verwijderen";
				deleteLink.CssClass = "common";
				deleteLink.NavigateUrl = HttpContext.Current.Request.Url.AbsolutePath + "?del=1&id=" + resultSet[row, "id"].ToString(); ;
				stateCell.Controls.Add(deleteLink);
			}

			tableRow.Cells.Add(stateCell);

			this.Table1.Rows.Add(tableRow);
		}

		private void CreateDetailRow(ResultSet resultSet, int row)
		{
			this.CreateDetailHeader(resultSet, row);

			this.CreateDetailBody(resultSet, row);

			this.CreateDetailBreak();
		}

		private void CreateDetailHeader(ResultSet resultSet, int row)
		{
			TableRow headerRow = new TableRow();
			headerRow.CssClass = "myadheader";

			this.CreateDetailHeaderCell(headerRow, "User");
			this.CreateDetailHeaderCell(headerRow, "Nummer");
			this.CreateDetailHeaderCell(headerRow, "Gestart");
			this.CreateDetailHeaderCell(headerRow, "Beëindigd");
			this.CreateDetailHeaderCell(headerRow, "# Vertoningen");
			this.CreateDetailHeaderCell(headerRow, "Verloopt bij");
			this.CreateDetailHeaderCell(headerRow, "# Kliks");
			this.CreateDetailHeaderCell(headerRow, "Laatste vertoning");
			this.CreateDetailHeaderCell(headerRow, "Laatste klik");
			this.CreateDetailHeaderCell(headerRow, "Kosten");

			this.Table1.Rows.Add(headerRow);
		}

		private void CreateDetailHeaderCell(TableRow headerRow, string text)
		{
			TableCell headerCell = new TableCell();

			headerCell.CssClass = "myadheader";
			headerCell.Text = text;

			headerRow.Cells.Add(headerCell);
		}

		private void CreateDetailBody(ResultSet resultSet, int row)
		{
			TableRow detailRow = new TableRow();
			detailRow.CssClass = "myaddetails";

			this.CreateDetailCell(detailRow, resultSet[row, "gebruikersnaam"].ToString());
			this.CreateDetailCell(detailRow, resultSet[row, "id"].ToString());
			this.CreateDetailCell(detailRow, (DateTime)resultSet[row, "startdate"]);
			this.CreateDetailCell(detailRow, (DateTime)resultSet[row, "enddate"]);
			this.CreateDetailCell(detailRow, resultSet[row, "views"].ToString());
			this.CreateDetailCell(detailRow, resultSet[row, "maxviews"].ToString());
			this.CreateDetailCell(detailRow, resultSet[row, "clicks"].ToString());
			this.CreateDetailCell(detailRow, (DateTime)resultSet[row, "lastview"]);
			this.CreateDetailCell(detailRow, (DateTime)resultSet[row, "lastclick"]);
			this.CreateDetailCell(detailRow, ObjectFormatter.GetCurrencyString(resultSet[row, "cost"]));

			this.Table1.Rows.Add(detailRow);
		}

		private void CreateDetailCell(TableRow tableRow, string text)
		{
			TableCell detailCell = new TableCell();

			detailCell.Text = text;
			detailCell.CssClass = "myaddetails";
			tableRow.Cells.Add(detailCell);
		}

		private void CreateDetailCell(TableRow tableRow, DateTime dt)
		{
			this.CreateDetailCell(tableRow, this.GetDateTimeString(dt));
		}

		private void CreateDetailBreak()
		{
			TableRow row = new TableRow();

			TableCell cell = new TableCell();
			cell.CssClass = "myadbreak";
			cell.ColumnSpan = 9;

			row.Cells.Add(cell);

			this.Table1.Rows.Add(row);
		}

		private string GetDateTimeString(DateTime dt)
		{
			string str = (dt.Year == 1980 ? " -- " : dt.ToString("dd-MM-yyyy, HH:mm:ss"));

			return str;
		}
	}
}